#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

/* int main()
{
    int fd_lcd; // 0-1023  012 我们自己是从3开始
    fd_lcd = open("/dev/fb0", O_RDWR);

    if (fd_lcd < 0)
    {
        printf("open lcd error: %s\n", strerror(errno));
        return -1;
    }
    printf("open lcd success\n");

    // 映射内存  lcd_mmap int类型的指针  800*480*4字节
    int *lcd_mmap = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd_lcd, 0);
    printf("mmap success\n");
    printf("lcd_mmap: %p %p %d\n", lcd_mmap, lcd_mmap + 1, sizeof(int));

    for (int i = 0; i < 800 * 480 / 3; i++)
    {
        lcd_mmap[i] = 0x00ff0000;
    }
    for (int i = 800 * 480 / 3; i < 800 * 480 / 3 * 2; i++)
    {
        lcd_mmap[i] = 0x0000ff00;
    }
    for (int i = 800 * 480 / 3 * 2; i < 800 * 480; i++)
    {
        lcd_mmap[i] = 0x000000ff;
    }

    return 0;
} */

int main()
{
    int fd_lcd; // 0-1023  012 我们自己是从3开始
    fd_lcd = open("/dev/fb0", O_RDWR);

    if (fd_lcd < 0)
    {
        printf("open lcd error: %s\n", strerror(errno));
        return -1;
    }
    printf("open lcd success\n");

    // 映射内存  lcd_mmap int类型的指针  800*480*4字节
    int (*lcd_mmap)[800] = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd_lcd, 0);
    printf("mmap success\n");
    printf("lcd_mmap: %p %p %d\n", lcd_mmap, lcd_mmap + 1, sizeof(int) * 800);

    // for (int row = 0; row < 480; row++)
    // {
    //     for (int col = 0; col < 800; col++)
    //     {
    //         lcd_mmap[row][col] = 0x00ff0000;
    //         // *(p + 1)  === p[1]
    //         // *(lcd_mmap + row) == lcd_mmap[row]
    //         // *(*(lcd_mmap + row) + col) == lcd_mmap[row][col]
    //     }
    // }

    for (int row = 0; row < 480; row++)
    {
        for (int col = 0; col < 800; col++)
        {
            if (col < 266)
            {
                lcd_mmap[row][col] = 0x00ff0000;
            }
            else if (col < 533)
            {
                lcd_mmap[row][col] = 0x0000ff00;
            }
            else
            {
                lcd_mmap[row][col] = 0x000000ff;
            }
        }
    }

    munmap(lcd_mmap, 800 * 480 * 4);
    close(fd_lcd);
    return 0;
}